<template>
	<view>
		<u-modal :show="show" :title="title" showCancelButton @confirm='confirmMethod' @cancel='cancelMethod'>
			<view class="slot-content" v-if='show'>
				<view class="personal-info-box">
					<view>
						<view style="display: flex;padding: 20rpx 0;">
							<view style="width: 200rpx;">医院评价</view>
							<view>
								<u--input v-model="model.storeContent"></u--input>
							</view>
						</view>
						<view style="display: flex;padding: 20rpx 0;">
							<view style="width: 200rpx;">医院评分</view>
							<view>
								<u-rate count="5" v-model="model.storeScore"></u-rate>
							</view>
						</view>
						<view style="display: flex;padding: 20rpx 0;">
							<view style="width: 200rpx;">医院评价图片</view>
							<view>
								<u-upload :file-list="storeList" @afterRead="afterRead" @delete="deletePic"
									:maxCount="5" :previewFullImage="true"></u-upload>
							</view>
						</view>
						<view style="display: flex;padding: 20rpx 0;">
							<view style="width: 200rpx;">医生评价</view>
							<view>
								<u--input v-model="model.doctorContent"></u--input>
							</view>
						</view>
						<view style="display: flex;padding: 20rpx 0;">
							<view style="width: 200rpx;">医生评分</view>
							<view>
								<u-rate count="5" v-model="model.doctorScore"></u-rate>
							</view>
						</view>
						<view style="display: flex;padding: 20rpx 0;">
							<view style="width: 200rpx;">医生评价图片</view>
							<view>
								<u-upload :file-list="doctorList" @afterRead="doctorafterRead" @delete="doctordeletePic"
									:maxCount="5" :previewFullImage="true"></u-upload>
							</view>
						</view>

					</view>
				</view>

			</view>
		</u-modal>
		<u-button @click="show = true" text="评论" color="#2E75E8" type="info">评价</u-button>
	</view>
</template>
<script>
	import baseUrl from "@/api/axios/baseUrl.js";
	import {
		evaluate
	} from '@/api/axios/MakeAnAppointmentCenter.js'
	export default {
		props: {
			appointStoreId: {
				type: String,
				required: true
			},
			doctorId: {
				type: String,
				required: true
			},
			kid: {
				type: String,
				required: true
			},
		},
		data() {
			return {
				show: false,
				title: '评论',
				model: {
					openId: "",
					storeId: "",
					doctorId: '',
					storeScore: 0,
					storeContent: '',
					storeImage: '',
					doctorScore: 0,
					doctorContent: '',
					doctorImage: '',
				},
				storeList: [],
				doctorList: [],


			}
		},
		methods: {
			cancelMethod() {
				this.show = false
			},
			confirmMethod() {
				let _this = this
				if (this.storeList.length > 0) {
					this.storeList.forEach(item => {
						this.model.storeImage += item.url + ',';
					})
				}
				if (this.doctorList.length > 0) {
					this.doctorList.forEach(item => {
						this.model.doctorImage += item.url + ',';
					})
				}
				console.log(this.model);
				this.model.storeEvaluate = {
					relationId: this.appointStoreId,
					score: this.model.storeScore,
					content: this.model.storeContent,
					image: this.model.storeImage,
				}
				this.model.doctorEvaluate = {
					relationId: this.doctorId,
					score: this.model.doctorScore,
					content: this.model.doctorContent,
					image: this.model.doctorImage,
				}
				console.log(this.model);
				evaluate(this.model).then((res) => {
					// uni.navigateBack({
					// 	delta: 1,
					// });
					_this.show = false
				});
			},
			// 删除图片
			deletePic(event) {
				this[`storeList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				console.log(event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				console.log(this[`storeList${event.name}`]);
				let storeListLen = this[`storeList${event.name}`].length;
				lists.map((item) => {
					this[`storeList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`storeList${event.name}`][storeListLen];
					this[`storeList${event.name}`].splice(
						storeListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					storeListLen++;
				}
			},

			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: baseUrl.url + "/xihefile/user/file/upload",
						filePath: url,
						name: "file",
						formData: {},
						success: (uploadFileRes) => {
							console.log(uploadFileRes);
							let url = JSON.parse(uploadFileRes.data).data;
							console.log(url, "url");
							resolve(url);
						},
					});
				});
			},
			// 新增图片
			async doctorafterRead(event) {
				console.log(event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				console.log(this[`doctorList${event.name}`]);
				let doctorListLen = this[`doctorList${event.name}`].length;
				lists.map((item) => {
					this[`doctorList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`doctorList${event.name}`][doctorListLen];
					this[`doctorList${event.name}`].splice(
						doctorListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					doctorListLen++;
				}
			},
			// 删除图片
			doctordeletePic(event) {
				this[`doctorList${event.name}`].splice(event.index, 1);
			},

		},
		mounted() {
			this.show = false
			this.model.openId = uni.getStorageSync("openId");
			this.model.id = this.kid;

		}

	}
</script>
<style lang="scss" scoped>

</style>